/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beanAdmin;

import com.matrimonysite.entity.Users;
import com.matrimonysite.sessionbean.UsersFacade;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import helper.MD5;
import helper.messages;
import javax.faces.application.FacesMessage;
/**
 *
 * @author Huynh Kim Toan
 */
@ManagedBean(name="CPMemberAndStaff")
@RequestScoped
public class changePassUserAndCustomer {
    @EJB
    private UsersFacade userFacade;
private String userName;
private String pass;

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
private Users login = new Users();

    public changePassUserAndCustomer() {
    }
    public void managePassMember()
    {
         
          try
          {
//              if(this.login.getRoles().equals("admin"))
//              {
//               messages.taoTB(FacesMessage.SEVERITY_ERROR, "Can't change password admin", "Error");
//              }else if(this.login.getRoles().equals("member") || this.login.getRoles().equals("staff")){
//                this.loginFacade.edit(login);
//                messages.taoTB(FacesMessage.SEVERITY_INFO, "Change Password Successfull!", "Successfull");
//              }else if(login == null)
//              {
//                messages.taoTB(FacesMessage.SEVERITY_ERROR, "User Name Không Tồn Tại", "Error");
//              }else
//              {
//                   messages.taoTB(FacesMessage.SEVERITY_ERROR, "Error", "Error");
//              }
//
//
              if(userName.length() ==0)
              {
                  messages.taoTB(FacesMessage.SEVERITY_ERROR, "User not null", "Error");
              }else if(pass.length()==0)
              {
                  messages.taoTB(FacesMessage.SEVERITY_ERROR, "Password not null", "Error");
              }
              else
              {
                   this.login = this.userFacade.getUserName(userName);
                   if(login == null)
                   {
                       messages.taoTB(FacesMessage.SEVERITY_ERROR, "User Name Not Exist", "Error");
                   }else if(this.login.getRoleID().equals("admin"))
                   {
                       messages.taoTB(FacesMessage.SEVERITY_ERROR, "Can't change password admin", "Error");
                   }else
                   {
                       if(pass.length() < 6)
                       {
                            messages.taoTB(FacesMessage.SEVERITY_ERROR, "Pass must be greater than 5 characters !", "error");
                       }
                      this.login.setPassword(MD5.getMD5(pass));
                      this.userFacade.edit(login);
                      messages.taoTB(FacesMessage.SEVERITY_INFO, "Change Password Successfull!", "Successfull");
                   }
              }
          }catch(Exception ex)
          {
               messages.taoTB(FacesMessage.SEVERITY_ERROR, "Change Password Error!", "Change Password");
          }
          
    }
      public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Users getLogin() {
        return login;
    }

    public void setLogin(Users login) {
        this.login = login;
    }

}
